/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;
import javax.enterprise.context.spi.Context;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;
/**
 *
 * @author Java
 */
public class Relatorio extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        BancoDao banco = new BancoDao();
        String caminhoRelatorio = "/WEB-INF/model/Usuario.jasper";
        
                
        
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
        }
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Desenvolver","root","desenvolver");
            System.out.println("conectado");
        } catch (Exception e) {
        }
        
        
        
        
        
        ServletContext context = getServletContext();
        File ReporFile = new File(caminhoRelatorio);
        byte[]bytes = null;
        try {
             
          JasperReport relatorio = (JasperReport)JRLoader.loadObject(context.getRealPath("/WEB-INF/Relatorio/Usuario.jasper"));
      //     bytes = JasperRunManager.runReportToPdf(relatorio, null, conn);
           
           Map parametros = new HashMap();
          
          bytes = JasperRunManager.runReportToPdf(relatorio, parametros, conn);
          
        } catch (Exception e) {
            
           System.out.println("não não não");
        }
        if (bytes != null && bytes.length >0){
            response.setContentType("application/pdf");
            response.setContentLength(bytes.length);
            ServletOutputStream ouputStream = response.getOutputStream();
            ouputStream.write(bytes, 0, bytes.length);
            ouputStream.flush();
            ouputStream.close();
            
            
        }
/*        
//        
        ServletContext sc = request.getSession().getServletContext();
        File reportFile = new File(sc.getRealPath(caminhoRelatorio));
        System.out.println("Bytes: " + reportFile.getUsableSpace());
        try {
            JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
        } catch (Exception e) {
        }
        
//      Gerar  
        
        byte[]bytes = null;
        try {
            bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), null, banco.MySql());
            
        } catch (Exception e) {
        }
        
        
//      imprimir
        try {
            if(bytes != null && bytes.length > 0){
                ServletOutputStream outputStream = response.getOutputStream();
                response.reset();
                response.setContentType("application/pdf");
                response.setContentLength(bytes.length);
                
                outputStream.write(bytes, 0, bytes.length);
                outputStream.flush();
                outputStream.close();
                
            }
        } catch (Exception e) {
        }
        
  */      
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        
        
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

  
}
